<?php

class Skjb_Office_Spreadsheet_Exporter_Excel2007Xml
{
    protected $_spreadsheet = null;
    
    protected $_zip = null;
    
    protected $_sharedStrings = null;
    
    public function __construct(Skjb_Office_Spreadsheet $spreadsheet)
    {
        $this->_spreadsheet = $spreadsheet;
    }
    
    protected function _addContentTypes()
    {
        $contentTypes = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_ContentTypes();
        
        $contentTypes
            ->addDefault('rels', 'application/vnd.openxmlformats-package.relationships+xml')
            ->addDefault('xml', 'application/xml')
            ->addOverride('/docProps/app.xml',         'application/vnd.openxmlformats-officedocument.extended-properties+xml')
            ->addOverride('/docProps/core.xml',        'application/vnd.openxmlformats-package.core-properties+xml')
            ->addOverride('/xl/sharedStrings.xml',     'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml')
            ->addOverride('/xl/styles.xml',            'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml')
            ->addOverride('/xl/theme/theme1.xml',      'application/vnd.openxmlformats-officedocument.theme+xml')
            ->addOverride('/xl/workbook.xml',          'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml')
            ->addOverride('/xl/worksheets/sheet1.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml')
        ;
        
        $this->_zip->addFromString('[Content_Types].xml', $contentTypes->toXml());
    }
    
    protected function _addRootRels()
    {
        $relationships = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_Relationships();
        $relationships
            ->addRelationship('rId1', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', 'xl/workbook.xml')
            ->addRelationship('rId2', 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', 'docProps/core.xml')
            ->addRelationship('rId3', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', 'docProps/app.xml')
        ;
        
        $this->_zip->addFromString('_rels/.rels', $relationships->toXml());
    }
    
    protected function _addDocPropsApp()
    {
        $applicationProperties = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_ApplicationProperties();
        
        $this->_zip->addFromString('docProps/app.xml', $applicationProperties->toXml());
    }
    
    protected function _addDocPropsCore()
    {
        $coreProperties = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_CoreProperties();
        
        $this->_zip->addFromString('docProps/core.xml', $coreProperties->toXml());
    }
    
    protected function _addSharedStrings()
    {
        $this->_sharedStrings = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_SharedStrings($this->_spreadsheet);
        
        $this->_zip->addFromString('xl/sharedStrings.xml', $this->_sharedStrings->toXml());
    }
    
    protected function _addStyles()
    {
        $styles = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_Styles();
        
        $this->_zip->addFromString('xl/styles.xml', $styles->toXml());
    }
    
    protected function _addWorkbook()
    {
        $workbook = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_Workbook();
        
        $this->_zip->addFromString('xl/workbook.xml', $workbook->toXml());
    }
    
    protected function _addWorkbookRels()
    {
        $relationships = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_Relationships();
        $relationships
            ->addRelationship('rId1', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet', 'worksheets/sheet1.xml')
            ->addRelationship('rId2', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', 'theme/theme1.xml')
            ->addRelationship('rId3', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles', 'styles.xml')
            ->addRelationship('rId4', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings', 'sharedStrings.xml')
        ;
        
        $this->_zip->addFromString('xl/_rels/workbook.xml.rels', $relationships->toXml());
    }
    
    protected function _addTheme()
    {
        $theme = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_Theme();
        
        $this->_zip->addFromString('xl/theme/theme1.xml', $theme->toXml());
    }
    
    protected function _addSheet($inSheet)
    {
        $sheet = new Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_Sheet($inSheet, $this->_sharedStrings);
        
        $this->_zip->addFromString('xl/worksheets/sheet1.xml', $sheet->toXml());
    }
    
    public function getRawExport()
    {
        $this->_zip = new ZipArchive();
        $this->_zip->open('test.zip', ZipArchive::CREATE);
        
        $this->_zip->addEmptyDir('_rels');
        $this->_zip->addEmptyDir('docProps');
        $this->_zip->addEmptyDir('xl');
        $this->_zip->addEmptyDir('xl/_rels');
        $this->_zip->addEmptyDir('xl/printerSettings');
        $this->_zip->addEmptyDir('xl/theme');
        $this->_zip->addEmptyDir('xl/worksheets');
        //$this->_zip->addEmptyDir('xl/worksheets/_rels');
        
        $this->_addContentTypes();
        $this->_addRootRels();
        $this->_addDocPropsApp();
        $this->_addDocPropsCore();
        $this->_addSharedStrings();
        $this->_addStyles();
        $this->_addWorkbook();
        $this->_addWorkbookRels();
        $this->_addTheme();
        foreach ($this->_spreadsheet->sheets as $sheet) {
            $this->_addSheet($sheet);
        }
        
        $this->_zip->close();
        
        $contents = file_get_contents('test.zip');
        unlink('test.zip');
        
        return $contents;
    }
}